package com.sskj.gcc.market.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sskj.common.base.App;
import com.sskj.gcc.market.ChartFragment;
import com.sskj.gcc.market.DepthFragment;
import com.sskj.gcc.market.data.MarketDetail;
import com.sskj.gcc.market.dialog.MorePopupWindow;
import com.sskj.gcc.market.utils.NumberUtils;
import com.sskj.gcc.ui.fragment.TradeFragment;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.bean.CoinDotBean;
import com.sskj.lib.bean.ColorEnum;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.widget.KChartPop;
import com.sskj.lib.widget.tab.TabItem;
import com.sskj.lib.widget.tab.TabLayout;
import com.sskj.lib.widget.tab.TabSelectListener;
import com.sskj.market.R;

import java.util.ArrayList;
import java.util.List;

public class MarketDetailAdapter extends BaseMultiItemQuickAdapter<MarketDetail, BaseViewHolder> {


    private String code;

    //    private AppCompatActivity mFragmentManager;
    private FragmentActivity activity;

    ArrayList<TabItem> chartTabs = new ArrayList<>();

    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    String[] goodsType = {"minute", "minute15", "minute60", "hour4", "day", "minute", "minute5", "minute30", "week", "month"};

    //ChartTab
    TabLayout chartTabLayout;

    //底部Tab
    TabLayout bottomTabLayout;

    //指标弹窗
    private KChartPop kChartPop;

    private ArrayList<Fragment> bottomFragments = new ArrayList<>();

    private ArrayList<TabItem> bottomTabs = new ArrayList<>();

    private LinearLayout chartNorm;

    //更多弹框
    private MorePopupWindow morePopuWindow;

    //指标
    private boolean isUpToggle = true;

    private boolean isDownToggle = true;

    public MarketDetailAdapter(List<MarketDetail> data, FragmentActivity activity) {
        super(data);
        this.activity = activity;
        addItemType(MarketDetail.TOP, R.layout.market_detail_top);
        addItemType(MarketDetail.CHART, R.layout.market_detail_chart);
        addItemType(MarketDetail.BOTTOM, R.layout.market_detail_bottom);
    }


    @Override
    protected void convert(BaseViewHolder helper, MarketDetail item) {
        switch (helper.getItemViewType()) {
            case MarketDetail.TOP:
                if (item.getTopData() != null) {
                    code = item.getTopData().getCode();
                    helper.setText(R.id.tv_price, CoinUtil.keepCoinPrice(item.getTopData().getCode(), item.getTopData().getPrice()))
                            .setText(R.id.tv_change_rate, item.getTopData().isUp() ? "+" + item.getTopData().getChangeRate() : item.getTopData().getChangeRate())
                            .setTextColor(R.id.tv_change_rate, ContextCompat.getColor(App.INSTANCE, item.getTopData().isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()))
                            .setText(R.id.tvCny, "≈" + NumberUtils.keepDown(item.getTopData().getCnyPrice(), 2) + "CNY")
                            .setText(R.id.tv_high, CoinUtil.keepCoinPrice(item.getTopData().getCode(), item.getTopData().getHigh()))
                            .setText(R.id.tv_low, CoinUtil.keepCoinPrice(item.getTopData().getCode(), item.getTopData().getLow()));
                    if (Double.parseDouble(item.getTopData().getChange()) > 0) {
                        helper.setTextColor(R.id.tv_price, ContextCompat.getColor(mContext, R.color.market_green));
                    } else if (Double.parseDouble(item.getTopData().getChange()) == 0) {
                        helper.setTextColor(R.id.tv_price, ContextCompat.getColor(mContext, R.color.commonText333));
                    } else {
                        helper.setTextColor(R.id.tv_price, ContextCompat.getColor(mContext, R.color.market_red));
                    }
                }
                break;
            case MarketDetail.CHART:
                if (chartTabLayout == null) {
                    initTab();
                    chartNorm = helper.getView(R.id.chart_norm);
                    chartTabLayout = helper.getView(R.id.chart_tabLayout);
                    chartTabLayout.setTabData(chartTabs);
                    chartTabLayout.setOnTabSelectListener(new TabSelectListener() {
                        @Override
                        public boolean onTabSelect(int position) {
                            if (position == 5) {
                                showMore();
                                return false;
                            } else {
                                setFragments(position);
                            }
                            return true;
                        }

                        @Override
                        public boolean onTabReselect(int position) {
                            if (position == 5) {
                                showMore();
                            }
                            return true;
                        }
                    });
                    initFragments();
                    initPoint();
                }
                break;
            case MarketDetail.BOTTOM:
                if (bottomTabLayout == null) {
                    initBottomTab();
                    bottomTabLayout = helper.getView(R.id.bootmTabLyout);
                    bottomTabLayout.setTabData(bottomTabs, activity.getSupportFragmentManager(), R.id.order_content, bottomFragments);
                }
                break;
            default:
                break;
        }
    }


    private void initTab() {
        chartTabs.add(new TabItem(App.INSTANCE.getString(R.string.market_time), 0, 0));
        chartTabs.add(new TabItem("15M", 0, 0));
        chartTabs.add(new TabItem("1H", 0, 0));
        chartTabs.add(new TabItem("4H", 0, 0));
        chartTabs.add(new TabItem(App.INSTANCE.getString(R.string.market_day), 0, 0));
        chartTabs.add(new TabItem(mContext.getString(R.string.market_more), 0, 0));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[0], true));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[1], false));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[2], false));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[3], false));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[4], false));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[5], false));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[6], false));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[7], false));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[8], false));
        fragmentList.add(ChartFragment.newInstance(code, goodsType[9], false));
    }

    private void initBottomTab() {
        bottomTabs.add(new TabItem(App.INSTANCE.getString(R.string.marketmarketDetailAdapter1), 0, 0));
        bottomTabs.add(new TabItem(App.INSTANCE.getString(R.string.market_deal), 0, 0));
        bottomTabs.add(new TabItem(App.INSTANCE.getString(R.string.xmlfragment_summary150), 0, 0));
        bottomFragments.add(DepthFragment.newInstance(code));
        bottomFragments.add(TradeFragment.newInstance(code));
        bottomFragments.add((Fragment) ARouter.getInstance().build(RConfig.APP_FRAGMENT_SUMMARY).withString("code", code).navigation());
    }


    private void initPoint() {
        ClickUtil.click(chartNorm, () -> {
            if (kChartPop == null) {
                kChartPop = new KChartPop(mContext);
                View contentView = kChartPop.getContentView();
                TextView tvMa = contentView.findViewById(R.id.tvMa);
                TextView tvBoll = contentView.findViewById(R.id.tvBoll);
                TextView tvMacd = contentView.findViewById(R.id.tvMacd);
                TextView tvKdj = contentView.findViewById(R.id.tvKdj);
                TextView tvRsi = contentView.findViewById(R.id.tvRsi);
                TextView tvWr = contentView.findViewById(R.id.tvWr);
                ImageView ivUpToggle = contentView.findViewById(R.id.ivUpToggle);
                ImageView ivDownToggle = contentView.findViewById(R.id.ivDownToggle);
                ivUpToggle.setImageResource(R.mipmap.market_icon_show);
                ivDownToggle.setImageResource(R.mipmap.market_icon_show);
                ClickUtil.click(tvMa, () -> {
                    for (int i = 0; i < fragmentList.size(); i++) {
                        ((ChartFragment) fragmentList.get(i)).getKChartView().setMainDrawMaShow();
                    }
//                    ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().setMainDrawMaShow();
                    unselected1(tvMa, tvBoll);
                    tvMa.setTextColor(activity.getResources().getColor(R.color.commonSelectColor));
                    ivUpToggle.setImageResource(R.mipmap.market_icon_show);
                    kChartPop.dismiss();
                });
                ClickUtil.click(tvBoll, () -> {
                    for (int i = 0; i < fragmentList.size(); i++) {
                        ((ChartFragment) fragmentList.get(i)).getKChartView().setMainDrawBollShow();
                    }
//                    ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().setMainDrawBollShow();
                    unselected1(tvMa, tvBoll);
                    tvBoll.setTextColor(activity.getResources().getColor(R.color.commonSelectColor));
                    ivUpToggle.setImageResource(R.mipmap.market_icon_show);
                    kChartPop.dismiss();

                });
                ClickUtil.click(ivUpToggle, () -> {
                    if (isUpToggle) {
                        isUpToggle = false;
                        ivUpToggle.setImageResource(R.mipmap.market_icon_hide);
                        for (int i = 0; i < fragmentList.size(); i++) {
                            ((ChartFragment) fragmentList.get(i)).getKChartView().setMainDrawNoneShow();
                        }
//                        ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().setMainDrawNoneShow();
                    } else {
                        isUpToggle = true;
                        ivUpToggle.setImageResource(R.mipmap.market_icon_show);
                        for (int i = 0; i < fragmentList.size(); i++) {
                            ((ChartFragment) fragmentList.get(i)).getKChartView().setMainDrawMaShow();
                        }
//                        ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().setMainDrawMaShow();
                    }
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvMacd, () -> {
                    for (int i = 0; i < fragmentList.size(); i++) {
                        ((ChartFragment) fragmentList.get(i)).getKChartView().changeMACD();
                    }
//                    ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().changeMACD();
                    unselected2(tvMacd, tvKdj, tvRsi, tvWr);
                    tvMacd.setTextColor(activity.getResources().getColor(R.color.commonSelectColor));
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvKdj, () -> {
                    for (int i = 0; i < fragmentList.size(); i++) {
                        ((ChartFragment) fragmentList.get(i)).getKChartView().changeKDJ();
                    }
//                    ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().changeKDJ();
                    unselected2(tvMacd, tvKdj, tvRsi, tvWr);
                    tvKdj.setTextColor(activity.getResources().getColor(R.color.commonSelectColor));
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvRsi, () -> {
                    for (int i = 0; i < fragmentList.size(); i++) {
                        ((ChartFragment) fragmentList.get(i)).getKChartView().changeRSI();
                    }
//                    ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().changeRSI();
                    unselected2(tvMacd, tvKdj, tvRsi, tvWr);
                    tvRsi.setTextColor(activity.getResources().getColor(R.color.commonSelectColor));
                    kChartPop.dismiss();

                });
                ClickUtil.click(tvWr, () -> {
                    for (int i = 0; i < fragmentList.size(); i++) {
                        ((ChartFragment) fragmentList.get(i)).getKChartView().changeWR();
                    }
//                    ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().changeWR();
                    unselected2(tvMacd, tvKdj, tvRsi, tvWr);
                    tvWr.setTextColor(activity.getResources().getColor(R.color.commonSelectColor));
                    kChartPop.dismiss();

                });
                ClickUtil.click(ivDownToggle, () -> {
                    if (isDownToggle) {
                        isDownToggle = false;
                        ivDownToggle.setImageResource(R.mipmap.market_icon_hide);
                        for (int i = 0; i < fragmentList.size(); i++) {
                            ((ChartFragment) fragmentList.get(i)).getKChartView().setDrawDown(isDownToggle);
                        }
//                        ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().setDrawDown(isDownToggle);
                    } else {
                        isDownToggle = true;
                        ivDownToggle.setImageResource(R.mipmap.market_icon_show);
                        for (int i = 0; i < fragmentList.size(); i++) {
                            ((ChartFragment) fragmentList.get(i)).getKChartView().setDrawDown(isDownToggle);
                        }
//                        ((ChartFragment) fragmentList.get(chartTabLayout.getCurrentTab())).getKChartView().setDrawDown(isDownToggle);
                    }
                    kChartPop.dismiss();

                });
                kChartPop.setBackground(0);
            }
            if (kChartPop.isShowing()) {
                kChartPop.dismiss();
            } else {
                kChartPop.showPopupWindow(chartTabLayout);
            }
        });
    }

    /**
     * 主图字体颜色
     */
    private void unselected1(TextView tv1, TextView tv2) {
        tv1.setTextColor(activity.getResources().getColor(R.color.common222324));
        tv2.setTextColor(activity.getResources().getColor(R.color.common222324));
    }

    /**
     * 副图字体颜色
     */
    private void unselected2(TextView tv1, TextView tv2, TextView tv3, TextView tv4) {
        tv1.setTextColor(activity.getResources().getColor(R.color.common222324));
        tv2.setTextColor(activity.getResources().getColor(R.color.common222324));
        tv3.setTextColor(activity.getResources().getColor(R.color.common222324));
        tv4.setTextColor(activity.getResources().getColor(R.color.common222324));
    }

    /**
     * 初始化fragments
     */
    private void initFragments() {
        for (Fragment fragment : fragmentList) {
            activity.getSupportFragmentManager().beginTransaction()
                    .add(R.id.chart_content, fragment, fragment.getClass().getSimpleName())
                    .hide(fragment)
                    .commit();
        }
        setFragments(0);
    }

    /**
     * 界面切换控制
     */
    public void setFragments(int index) {
        for (int i = 0; i < fragmentList.size(); i++) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
            Fragment fragment = fragmentList.get(i);
            if (i == index) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commit();
        }
    }

    private void showMore() {
        if (morePopuWindow == null) {
            morePopuWindow = new MorePopupWindow(mContext);
            morePopuWindow.setListener((popupWindow, type, name) -> {

                switch (type) {
                    case "minute":
                        setFragments(5);
                        break;
                    case "minute5":
                        setFragments(6);
                        break;
                    case "minute30":
                        setFragments(7);
                        break;
                    case "week":
                        setFragments(8);
                        break;
                    case "month":
                        setFragments(9);
                        break;
                    default:
                        break;
                }
                chartTabLayout.setCurrentTab(5);
                chartTabLayout.setTabTitle(name, 5);
                morePopuWindow.dismiss();
            });
        }
        morePopuWindow.showPopupWindow(chartTabLayout);
    }


}
